package dao;// EmployeeDAO.java
import pojo.Employee;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDAO {

    // 添加员工信息
    public boolean addEmployee(Employee emp) {
        String sql = "INSERT INTO employees(employee_id, name, gender, age, department, position, join_date, phone, email) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, emp.getEmployeeId());
            pstmt.setString(2, emp.getName());
            pstmt.setString(3, emp.getGender());
            pstmt.setInt(4, emp.getAge());
            pstmt.setString(5, emp.getDepartment());
            pstmt.setString(6, emp.getPosition());
            pstmt.setDate(7, new java.sql.Date(emp.getJoinDate().getTime()));
            pstmt.setString(8, emp.getPhone());
            pstmt.setString(9, emp.getEmail());
            int affected = pstmt.executeUpdate();
            return affected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 修改员工信息（根据员工编号定位记录）
    public boolean updateEmployee(Employee emp) {
        String sql = "UPDATE employees SET name=?, gender=?, age=?, department=?, position=?, join_date=?, phone=?, email=? " +
                "WHERE employee_id=?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, emp.getName());
            pstmt.setString(2, emp.getGender());
            pstmt.setInt(3, emp.getAge());
            pstmt.setString(4, emp.getDepartment());
            pstmt.setString(5, emp.getPosition());
            pstmt.setDate(6, new java.sql.Date(emp.getJoinDate().getTime()));
            pstmt.setString(7, emp.getPhone());
            pstmt.setString(8, emp.getEmail());
            pstmt.setString(9, emp.getEmployeeId());
            int affected = pstmt.executeUpdate();
            return affected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 删除员工信息（根据员工编号）
    public boolean deleteEmployee(String employeeId) {
        String sql = "DELETE FROM employees WHERE employee_id = ?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, employeeId);
            int affected = pstmt.executeUpdate();
            return affected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 根据员工编号获取员工信息
    public Employee getEmployeeById(String employeeId) {
        String sql = "SELECT * FROM employees WHERE employee_id = ?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, employeeId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return new Employee(
                        rs.getString("employee_id"),
                        rs.getString("name"),
                        rs.getString("gender"),
                        rs.getInt("age"),
                        rs.getString("department"),
                        rs.getString("position"),
                        rs.getDate("join_date"),
                        rs.getString("phone"),
                        rs.getString("email")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 按条件查询员工信息（例如：通过员工编号、姓名或部门）
    // 传入条件如 "employee_id = 'E001'" 或 "name LIKE '%张%'" 等
    public List<Employee> queryEmployeesByCondition(String condition) {
        List<Employee> list = new ArrayList<>();
        String sql = "SELECT * FROM employees WHERE " + condition;
        try (Connection conn = DBConnection.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Employee emp = new Employee(
                        rs.getString("employee_id"),
                        rs.getString("name"),
                        rs.getString("gender"),
                        rs.getInt("age"),
                        rs.getString("department"),
                        rs.getString("position"),
                        rs.getDate("join_date"),
                        rs.getString("phone"),
                        rs.getString("email")
                );
                list.add(emp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
